*******************************************************************
*Replication code for
*	Article: How COVID-19 affects voting for incumbents: evidence from local elections in France
*	Journal: PlosOne
*	Authors: Davide Morisi, Héloïse Cloléry, Guillaume Kon Kam King, and Max Schaub
*******************************************************************

clear all
cap log close
set more off

*cd "[your working directory]"


*** Paths
global pathdo "do_files"
global pathdata "data"
global pathtables "tables"
global pathfigures "figures"


tempfile finalData
use "$pathdata\Electoral_data.dta"

*Add population data:
replace NewCode = "13055" if NewCode=="13055SR01"
replace NewCode = "69123" if NewCode=="69123SR01"
merge m:m NewCode using "$pathdata\Extrapolated population.dta", keepusing(Pred_Population2020)
drop if _merge == 2
drop _merge
drop Code
rename NewCode Code

*Add census data:
merge m:m Code using "$pathdata\Census data.dta", keepusing(MaleToFemaleRatio PopDensity shareAbove80 shareAbove75 shareAbove65 shareImmigrants shareBlueCollar shareUnemp MedianStandardLiving PovertyRate ShareWelfare)
drop if _merge==2
drop _merge


****
*Population density from string to numeric
sum PopDensity
des PopDensity

merge 1:1 Code using "$pathdata\PopDensity_numeric.dta" 
drop if _merge==2
drop PopDensity PopDensity_orig
rename PopDensity_numeric PopDensity
drop _merge

order vote_share_Incumbent_2014 vote_share_Incumbent_2020 vote_share_IncumbList_2014 vote_share_IncumbList_2020, after (PopDensity)

rename Codedudépartement DepartmentCode
rename Codedelacommune MunicipalityCode

drop if DepartmentCode=="" & DepartmentName=="" & MunicipalityCode=="" & MunicipalityName=="" //drop empty observations

assert Year_data == "2020" if N_candidates_2014_1st==0
assert Year_data == "2014" if N_candidates_2020_1st==0

*solve the number of observations:
egen depmun_code = concat(DepartmentCode MunicipalityCode)
encode DepartmentCode, gen(DepartmentCodeEn)
drop if DepartmentCodeEn>96 // drop territories outside inland France if any

bys *: gen dup = cond(_N==1,0,_n)
drop if dup>1

*add census data for 2014
merge 1:1 municip using "$pathdata\CensusData2014.dta"
assert _merge==3
drop _merge
*save `finalData'
save "$pathdata\Covid19_Incumbency_Dataset.dta", replace

*add education data
import delimited "$pathdata\EducationData.csv", varnames(3) clear
rename (v5 v6 partdespersdontlediplômeleplusél partdesdiplômésdunbac2danslapopn partdesdiplômésdunbac3oubac4dans partdesdiplômésdunbac5ouplusdans) Education#, addnumber
rename partdesnonoupeudiplômésdanslapop Education0
foreach v of var Education* {
	replace `v' = "" if `v'=="N/A - résultat non disponible" | `v'=="N/A - division par 0"
	destring `v', replace
}
egen total = rowtotal(Education*)
replace total = round(total)
assert total==100 if Education0!=.
drop total libellé
//7 levels of education corresponding to the shares of inhabitants having different degrees. From Education0 = no diploma to Education6 = master degree or more.
rename code municip
*merge 1:1 municip using `finalData'
merge 1:1 municip using "$pathdata\Covid19_Incumbency_Dataset.dta"
drop if _merge==1
drop _merge //merge==2, missing info for arrondissements in Paris, Marseille & Lyon


*add our estimated measure of prevalence:
merge 1:1 municip using "$pathdata\prevalenceCovid.dta"
assert _merge==3
drop _merge
gen comment_MortalityData=""
*additional measures of prevalence (different time window)
merge 1:1 DepartmentCode MunicipalityCode using "$pathdata\prevalenceCovid_additional.dta"
drop if _merge==2 // 239 oversea municipalities
drop _merge

save "$pathdata\Covid19_Incumbency_Dataset.dta", replace
*end of do-file